package com.yanqu.road.server.timer.group;

import com.yanqu.road.entity.enums.eServerGroup2Type;
import com.yanqu.road.entity.servercenter.ServerGroup2;
import com.yanqu.road.logic.cross.CrossGroupMammonRecordListModel;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.group.CrossGroupProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.rank.CrossRankMgr;
import com.yanqu.road.server.manger.ServerGroup2Mgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.timer.GeneralAgentTimerMgr;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SyncAreaGroupMammonRecordTask extends GeneralTask {

    private boolean isRun = false;

    private long lastCheckTime;

    @Override
    public void doRun() {
        if (!isRun) {
            isRun = true;
            long nowTime = System.currentTimeMillis();
            List<CrossGroupMammonRecordListModel> syncList = new ArrayList<>();
            Map<Integer, Map<Integer, CrossGroupMammonRecordListModel>> allDataMap = CrossRankMgr.getAllCrossAreaGroupMammonRecordListModelMap();
            if (nowTime - lastCheckTime > GeneralAgentTimerMgr.ALL_SYNC_TIME) {
                for (Map<Integer, CrossGroupMammonRecordListModel> dataMap : allDataMap.values()) {
                    syncList.addAll(dataMap.values());
                }
                lastCheckTime = nowTime;
            } else {
                for (Map<Integer, CrossGroupMammonRecordListModel> dataMap : allDataMap.values()) {
                    for (CrossGroupMammonRecordListModel rankListModel : dataMap.values()) {
                        if (rankListModel.isNeedSync()) {
                            syncList.add(rankListModel);
                        }
                    }
                }
            }
            if (syncList.size() > 0) {
                for (CrossGroupMammonRecordListModel rankListModel : syncList) {
                    ServerGroup2 serverGroup2 = ServerGroup2Mgr.getServerGroup2(eServerGroup2Type.CrossAreaMammon, rankListModel.getGroupId());
                    CrossGroupProto.CrossGroupMammonRecordListMsg.Builder rankMsg = CrossGroupProto.CrossGroupMammonRecordListMsg.newBuilder();
                    rankMsg.setDecorationId(rankListModel.getMammonDecorationId());
                    rankMsg.setDataList(rankListModel.getRankListMsg());
                    rankListModel.setNeedSync(false);
                    syncToGameServer(rankMsg, serverGroup2);
                }
            }
            isRun = false;
        }
    }

    private void syncToGameServer(CrossGroupProto.CrossGroupMammonRecordListMsg.Builder rankMsg, ServerGroup2 serverGroup2) {
        if (serverGroup2 == null) {
            return;
        }
        for (long serverId : serverGroup2.getServerList()) {
            MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_CROSS_GROUP_SYNC_MAMMON_RECORD, rankMsg));
        }
    }
}
